package com.yoomiito.app.ui.order.sureorder.singleGoods

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.jakewharton.rxbinding3.view.clicks
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.bean.AddressInfo
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.model.order.OrderingInfo
import com.yoomiito.app.pay.PayResult
import com.yoomiito.app.pay.PayUtilsManager
import com.yoomiito.app.ui.anyview.sureorder.*
import com.yoomiito.app.ui.order.sureorder.BuyResultActivity
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.act_sure_order.*
import kotlinx.android.synthetic.main.title_bar.*
import java.util.concurrent.TimeUnit

/**
 * 仅星点订单使用
 */
class SureOrderActivity : BaseActivity<SureOrderPresenter>() {

    private var goodsType: Int = 0
    private lateinit var eventId: String
    private lateinit var mBaseOrderGoodsView: BaseSureOrderView
    private lateinit var mGoodsDetail: GoodsDetail

    companion object {
        fun start(context: Context, goodsDetail: GoodsDetail, goodsType: Int) {
            val intent = Intent(context, SureOrderActivity::class.java)
            intent.putExtra(Constant.KEY, goodsDetail)
            intent.putExtra(Constant.TYPE, goodsType)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_sure_order

    override fun newP(): SureOrderPresenter = SureOrderPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        iv_back_left.setOnClickListener { finish() }
        tv_center.text = "确认订单"
        mGoodsDetail = intent.getSerializableExtra(Constant.KEY) as GoodsDetail
        goodsType = intent.getIntExtra(Constant.TYPE, 0)

        initView(goodsType)

        act_buy_goods_btn.clicks()
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    if (mBaseOrderGoodsView.mAddressInfo == null) {
//                        TouchUtils.onBuyEvent(eventId, UserInfoConfig.getRoleId(), UserInfoConfig.getAgent(), "empty_address")
                        YMToast.showText("请添加地址")
                        return@subscribe
                    }
                    showLoading()
                    val orderInfo = mBaseOrderGoodsView.orderData()
                    when (goodsType) {
                        AllCode.StarStore -> p.createOrder(
                                orderInfo.goods[0].product_id.toString(),
                                orderInfo.name,
                                orderInfo.provinceCode,
                                orderInfo.cityCode,
                                orderInfo.countryCode,
                                orderInfo.area,
                                orderInfo.tel,
                                orderInfo.detailAddress,
                                orderInfo.goods[0].notes,
                                orderInfo.goods[0].num.toString()
                        )
                    }
                }
        showLoading()
        p.getAddress()
    }


    private fun initView(type: Int) {
        when (type) {
//            AllCode.YouPin, AllCode.AgentMall -> {
//                mBaseOrderGoodsView = YouPinOrderGoodsView(this, mGoodsDetail)
//                showLoading()
////                p!!.getRemain()
//            }
//            AllCode.BaoPin -> {
//                mBaseOrderGoodsView = BaoPinPinOrderGoodsView(this, mGoodsDetail)
//                act_buy_goods_money.text = mBaseOrderGoodsView.getSpanText()
//                eventId = "hotProductConfirmOrderPagePayClick"
//            }
//            AllCode.Gift -> {
//                mBaseOrderGoodsView = GiftOrderGoodsView(this, mGoodsDetail)
//                act_buy_goods_money.text = mBaseOrderGoodsView.getSpanText()
//                eventId = "giftProductConfirmOrderPagePayClick"
//            }
            AllCode.StarStore -> {
                mBaseOrderGoodsView = StarStoreSureOrderView(this, mGoodsDetail)
                act_buy_goods_money.text = mBaseOrderGoodsView.spanText
            }
        }
        mBaseOrderGoodsView.mOnShowInfoListener = { spanText, goodsSize, yoMoneyPay ->

        }
        frameLayout.addView(mBaseOrderGoodsView)
    }

    fun showAddressView(addressInfo: AddressInfo){
        dismissLoading()
        mBaseOrderGoodsView.updateAddress(addressInfo)
    }

    fun pay(info: OrderingInfo) {
        dismissLoading()
        PayUtilsManager.instance .payResultListener = {
            if (PayResult.PaySuccess == it.payState) {
                val eventMessage = EventMessage()
                eventMessage.goodsType = goodsType
                BuyResultActivity.start(this, eventMessage)
                finish()
            }
        }
        PayUtilsManager.instance
                .init(this, goodsType)
                .toStarStorePay(info)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (Const.RequestCode.AddressCode == requestCode && data != null) {
            val mAddressResult = data.getSerializableExtra(Constant.DATA) as AddressInfo
            mBaseOrderGoodsView.updateAddress(mAddressResult)
        }
    }

    override fun onDestroy() {
        PayUtilsManager.instance.release()
        super.onDestroy()
    }
}